var that
var interval_obj = null
export default {

    data() {
        return this.datainit({
            info: {},
            golist: [],
            id: null,
            buynumber: 1,
            user: {
                isgroup: null,
                group: null
            },
            isjoin: false
        })
    },

    mounted: function () {
        that = this
        this.id = this.$route.query.id
        this.check()
        interval_obj = setInterval(function () {
            that.setendtime();
        }, 1000)
    },
    beforeDestroy: function () {
        clearInterval(interval_obj)
    },
    methods: {
        user_init(_user) {
            that.user = _user
            if (that.user.isgroup) {
                that.group_get()
            }
            that.$post("api/goods/get", {id: this.id}, function (data) {
                that.info = data
            })
            that.$post("api/go/listsbygoods", {id: this.id}, function (_list) {
                let current_time = Date.parse(new Date()) / 1000;
                for (let i = 0; i < _list.length; i++) {
                    _list[i].endtime_str = that.setendtime_core(_list[i]['end_timestamp'], current_time)
                }
                that.golist = _list
            })
        },
        opnumber(cnum) {
            let nnum = parseInt(this.buynumber) + parseInt(cnum)
            if (nnum > 0) {
                this.buynumber = nnum
            }
        },
        buy: function () {
            let buylist = [{id: that.id, buynumber: this.buynumber}]
            that.$post("api/order/pre", {goods_list: buylist, type: "normal"}, function (pre_token) {
                //   that.$router.push({path:'/pages/order/make',query:{pre_token:pre_token}});
                window.location.href = "/pages/order/make?pre_token=" + pre_token
            })
        },
        setendtime: function () {
            let current_time = Date.parse(new Date()) / 1000;
            //console.log(current_time)
            let _list = that.golist
            for (let i = 0; i < _list.length; i++) {
                _list[i].endtime_str = that.setendtime_core(_list[i]['end_timestamp'], current_time)
            }
        },
        setendtime_core: function (end_timestamp, current_time) {
            let time = end_timestamp - current_time;
            if (time < 0) {
                return "已结束"
            }
            let day = Math.floor(time / (60 * 60 * 24));
            let hour = Math.floor(time / (60 * 60)) - (day * 24);
            let minute = Math.floor(time / 60) - (day * 24 * 60) - (hour * 60);
            let second = Math.floor(time) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);
            if (day <= 9) day = '0' + day;
            if (hour <= 9) hour = '0' + hour;
            if (minute <= 9) minute = '0' + minute;
            if (second <= 9) second = '0' + second;
            return "剩:<span>" + day + "</span>天<span>" + hour + "</span>:<span>" + minute + "</span>:<span>" + second + "</span>"
        }
        , group_add: function () {
            that.$post("api/group_goods/add", {id: that.id}, function () {
                that.group_get()
            })
        },
        group_remove: function () {
            that.$post("api/group_goods/remove", {id: that.id}, function () {
                that.group_get()
            })
        },
        group_get: function () {
            that.$post("api/group_goods/get", {id: that.id}, function (data) {
                if (data != null) {
                    that.isjoin = true
                }else{
                    that.isjoin = false
                }
            })
        }


    }


}